import os
from pydantic_settings import BaseSettings
from typing import ClassVar, Dict, Any,Optional


## 项目根目录
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(__file__)), ".."))


class Settings(BaseSettings):
    ## 路径基础配置
    PROJECT_ROOT : str = PROJECT_ROOT
    LOG_DIR : str = os.path.join(PROJECT_ROOT, "logs")
    
    """应用配置"""
    DATETIME_FORMAT: str = "%Y-%m-%d %H:%M:%S"
    # 基础配置
    debug: bool = True

    ## 项目前端口的 运行地址
    cors_origins: list = ["http://localhost:3001", "http://127.0.0.1:3001",
                          "http://localhost:3000","http://127.0.0.1:3000",
                          "http://192.168.1.107:3000","http://192.168.1.107:3001",
                          "http://192.168.0.100:3000","http://192.168.0.100:3001"]
    ## fastapi 项目后端 运行地址
    APP_HOST : str = "0.0.0.0"
    APP_PORT : int = 8000

    # 认证配置
    enable_auth: bool = True  # 是否启用JWT认证
    auth_whitelist: list = ["/docs", "/openapi.json", "/redoc", "/auth/login"]  # 无需认证的路径  白名单
    
        # 数据库配置（分离配置）
    database_host: str = "localhost"
    database_port: int = None
    database_user: str = "root"
    database_password: str = "root123"
    database_name: str = "home-test"
    
    # 数据库其他配置
    database_pool_size: int = 10
    database_echo: bool = False
    database_auto_create: bool = True
    
    # 功能开关 - 已合并到上面
    
    # OpenAI配置
    openai_api_key: str = "your_openai_api_key_here"
    openai_base_url: str = "https://api.openai.com/v1"
    openai_model: str = "gpt-3.5-turbo"
    openai_max_tokens: int = 200000000
    openai_temperature: float = 0.8
    
    ### 秘钥相关配置 ###
    SECRET_KEY: str = None  # openssl rand -hex 32
    JWT_ALGORITHM: str = "HS256"
    JWT_ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 7  # 7 day


    
    # 功能模块控制配置  启用如下
    enable_database: bool = True  # 是否启用数据库功能
    enable_user_system: bool = True  # 是否启用用户系统
    enable_user_registration: bool = True  # 是否启用用户注册验证
    enable_auto_init_data: bool = True  # 是否自动初始化默认数据
    enable_operation_logging: bool = True  # 是否启用操作日志
    
    # 原系统功能模块开关
    enable_origin_features: bool = True  # 是否启用原系统功能
    enable_api_management: bool = True  # 是否启用API管理
    enable_dept_management: bool = True  # 是否启用部门管理
    enable_audit_log: bool = True  # 是否启用审计日志
    
    # 家庭功能模块开关
    enable_family_features: bool = True  # 是否启用家庭功能
    enable_family_account: bool = True  # 是否启用家庭账户管理
    enable_ai_features: bool = True  # 是否启用AI功能
    enable_blog: bool = True  # 是否启用博客功能
    enable_chat: bool = True  # 是否启用聊天功能
    enable_file_management: bool = True  # 是否启用文件管理
    enable_pet_monitor: bool = True  # 是否启用宠物监控
    enable_recipe: bool = True  # 是否启用食谱功能


    class Config:
        env_file = os.path.join(PROJECT_ROOT,".env")
        case_sensitive = False


# 全局配置实例
settings = Settings()
print(PROJECT_ROOT)
print(settings.database_password)
# Tortoise ORM 配置   数据库配置
TORTOISE_ORM  = {
    "connections": {
        "default": {
            "engine": "tortoise.backends.mysql",
            "credentials": {
                "host": settings.database_host,
                "port": settings.database_port,
                "user": settings.database_user,
                "password": settings.database_password,
                "database": settings.database_name,
                "charset": "utf8mb4",
                "minsize": 1,
                "maxsize": settings.database_pool_size,
            }
        }
    },
    "apps": {
        "models": {
            "models": None,
            "default_connection": "default",
        }
    },
    "use_tz": False,
    "timezone": "Asia/Shanghai"
}